home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Files / Keith's CatSearch / Read Me < prev   
Encoding:
Text File  |  1995-02-15  |  4.8 KB  |  130 lines  |  [TEXT/KAHL]

  1. In article <jhiggins-101293131825@144.212.1.91>, jhiggins@mathworks.com (John
  2. M. Higgins) wrote:
  3. > Is there any way to convince PBCatSearch to start its search in a
  4. > subdirectory?  The parameter block that this function requires has a
  5. > CatPosition record which is set to 0 to start searching at the root level. 
  6. > Inside Mac doesn’t give any clue as to what this value means.  Directory
  7. > IDs do not seem to work.  Perhaps the search does not descend through the
  8. > file hierarchy in an orderly fashion, or perhaps not.  Anyone know?
  9. >
  10.  
  11. OK. I finally decided to show the world that Taligent programmers indeed do
  12. know how to program, and I wrote something to address this need.
  13.  
  14. PBCatSearch doesn’t have any way to allow the programmer to specify a folder
  15. hierarchy to search within. It will let you specify a specific folder to look
  16. in, but not the sub-folders within that folder. The reason for this is that
  17. PBCatSearch searches the catalog file linearly, the format of which bears no
  18. resemblance to the folder hierarchy.
  19.  
  20. I’ve always felt that there are two approaches to coming up with a PBCatSearch
  21. that does what people are asking for:
  22.  
  23. 1) Search the entire hard disk for files with the characteristics you are
  24. interested in. For each file found, walk up the directory chain to see if the
  25. file is within the folder you’re interested in. To speed things up, cache the
  26. results of the chain walking so that you don’t have to perform them again if
  27. you find multiple files in the same sub-folder.
  28.  
  29. 2) Pre-flight the search by walking the directory structure you’re interested
  30. in. During the walk, record the dirID of each directory you find. Then do the
  31. full disk search. For each file you find, compare its dirID with the list you
  32. made.
  33.  
  34. I implemented both this weekend to see how well they worked. I first tried #1,
  35. thinking that, with appropriate caching, it would be faster. Not getting the
  36. performance I wanted, I then tried #2. This one turned out to be slightly
  37. faster for the test case I was using, and the code was much smaller. Here are
  38. some comparisons:
  39.  
  40. Look for all MPW tools...
  41. o ... on my 400 Meg internal drive: 7 seconds
  42. o ... within my MPW folder, using approach #1: 12 seconds
  43. o ... within my MPW folder, using approach #2: 10 seconds
  44.  
  45. The interface to this routine is:
  46.  
  47. OSErr PBCatSearchIn(CSParamBlockRec*, long dirID);
  48.  
  49. By the way, I also took this opportunity to write something I’d always wanted
  50. to do: a high-level interface to PBCatSearch. This CatSearch routine uses a
  51. varargs interface, and works surprisingly well! For instance, to find my MPW
  52. Shell file, I say:
  53.  
  54.         err = CatSearch(&numberFound,
  55.                         csVRefNum, (short) -2,
  56.                         csMatchPtr, &MPWSpec, (long) 1,
  57.                         csFullName, "\pMPW Shell",
  58.                         csFInfoFDType, 'APPL',    // or else it will find my alias!
  59.                         csEndList);
  60.  
  61. (I apologize to everyone for hard-coding my vRefNum in the call!)
  62.  
  63. This says: search for “MPW Shell”, which is an application on the specified
  64. volume. Find the first instance of it, and return the FSSpec in MPWSpec.
  65.  
  66. Now let’s say that you wanted to find all MPW tools on your volume. This could
  67. be done as follows:
  68.  
  69.         err = CatSearch(&numberFound,
  70.                         csVRefNum, (short) -2,
  71.                         csMatchPtr, pMatchBuffer, (long) kMaxMatches,
  72.                         csFInfoFDType, 'MPST',
  73.                         csFInfoFDCreator, 'MPS ',
  74.                         csEndList);
  75.         while ((err == noErr) || (err == eofErr))
  76.         {
  77.             for (loopy = 0; loopy < numberFound; ++loopy)
  78.             {
  79.                 ...process files....
  80.             }
  81.     
  82.             if (err == noErr)
  83.                 err = CatSearch(&numberFound, csContinue);
  84.             else
  85.                 break;
  86.         }
  87.  
  88. Note the use of the “csContinue” feature. CatSearch remembers the last set of
  89. values passed to it and will reuse them if you want. Even so, I’ve found the
  90. loop structure used here a little awkward, so I also implemented a
  91. “csInitOnly” option. This option will let you specify the search options, but
  92. not actually perform the search. You can then write a loop like the
  93. following:
  94.  
  95.         err = CatSearch(&numberFound,
  96.                         csVRefNum, (short) -2,
  97.                         csMatchPtr, pMatchBuffer, (long) kMaxMatches,
  98.                         csFInfoFDType, 'MPST',
  99.                         csFInfoFDCreator, 'MPS ',
  100.                         csInitOnly,    // <<<<<<<<------ this was added
  101.                         csEndList);
  102.     
  103.         while (err == noErr)
  104.         {
  105.             err = CatSearch(&numberFound, csContinue);
  106.             if ((err == noErr) || (err == eofErr))
  107.             {
  108.                 for (loopy = 0; loopy < numberFound; ++loopy)
  109.                 {
  110.                 ...process files....
  111.                 }
  112.             }
  113.         }
  114.  
  115. CatSearch also supports PBCatSearchIn, so you can specify a sub-directory
  116. search thusly:
  117.  
  118.         err = CatSearch(&numberFound,
  119.                         csVRefNum, MPWSpec.vRefNum,
  120.                         csMatchPtr, pMatchBuffer, (long) kMaxMatches,
  121.                         csFInfoFDType, 'MPST',
  122.                         csFInfoFDCreator, 'MPS ',
  123.                         csSearchInDirectory, MPWSpec.parID,    // <<<<<<<<------ this was added
  124.                         csInitOnly,
  125.                         csEndList);
  126.  
  127. --------------------------------------------------------------------------
  128. Keith Rollin      ---      Phantom Programmer      ---      Taligent, Inc.
  129.